<template>
  <div class="container">
    <div class="screen-container">
      <!-- 搜索框 -->
      <search-form
        :data="queryParams"
        :field-list="filterInfo.fieldList"
        :list-type-info="labelObj"
        @handleSearch="handleQuery"
        @handleReset="resetQuery"
      />
    </div>
    <el-row :gutter="10">
      <el-button
        type="primary"
        icon="el-icon-plus"
        size="mini"
        @click="handleAdd"
      >新建</el-button>
      <el-button
        icon="el-icon-delete"
        size="mini"
        :disabled="ids.length == 0"
        @click="handleDelete"
      >删除</el-button>
      <el-button size="mini" @click="handleImport">导入 </el-button>
      <el-button
        size="mini"
        @click="handleExport"
      >导出
      </el-button>
    </el-row>
    <HkTable
      :loading="loading"
      :table-data="tableList"
      :table-columns="tableFieldList"
      :show-pag="true"
      :show-index="true"
      :query-params="queryParams"
      :total="total"
      :dict-list="labelObj"
      :operate="true"
      :show-overflow-tooltip="true"
      :selection="true"
      :select-fn="selectFn"
      table-height="80"
      @handleSelectionChange="handleSelectionChange"
      @changePage="getListData"
    >
      <template #flowName="scope">
        <el-button style="width:100%;display:inline-block;overflow:hidden;text-overflow:ellipsis;text-align:left;" type="text" @click="handleView(scope.row)">{{ scope.row.flowName }}</el-button>
      </template>
      <template #operate="scope">
        <el-button v-if="scope.row.enable == 0 && scope.row.status !== 0" type="text" @click="handleupdate(scope.row)">编辑</el-button>
        <el-button type="text" @click="handleCopy(scope.row)">复制</el-button>
        <el-button v-if="scope.row.enable == 0 && scope.row.status == 1" type="text" @click="handleOffline(scope.row)">永久下线</el-button>
        <el-button v-if="scope.row.enable == 0 && scope.row.status == 1" type="text" @click="handleEnable(scope.row)">启用</el-button>
        <el-button v-if="scope.row.enable == 1 && scope.row.status == 1" type="text" @click="handleDisenable(scope.row)">禁用</el-button>
        <el-button v-if="scope.row.enable == 0 && scope.row.status == 2" type="text" @click="handlePublish(scope.row)">发布</el-button>
      </template>
    </HkTable>
    <import-dialog ref="import_dia" @handleSubmit="resetQuery" />
    <export-dialog ref="export_dia" />
  </div>
</template>

<script>
import SearchForm from '@/components/HkSearchform'
import HkTable from '@/components/HkTable'
import importDialog from './importDialog.vue'
import exportDialog from './exportDialog.vue'
import { flowPageList, deleteFlow, flowDisable, flowEnable, flowOffline, flowPublish } from '@/api/ticket/processDesign'
import { download } from '@/utils/request'

export default {
  components: { SearchForm, HkTable, importDialog, exportDialog },
  beforeRouteLeave(to, from, next) {
    from.meta.keepAlive = false
    next()
  },
  data() {
    return {
      // 新增框是否 可见
      open: false,
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,
      // 弹出层标题
      title: '',
      // 选中的id值
      ids: [],
      // 查询参数
      queryParams: {
        size: 10,
        current: 1
      },
      // 查询
      filterInfo: {
        fieldList: [
          {
            label: '启用',
            type: 'select',
            field: 'enable',
            list: 'enableList'
          },
          {
            label: '发布状态',
            type: 'select',
            field: 'status',
            list: 'statusList'
          },
          // {
          //   label: '系统',
          //   type: 'select',
          //   field: 'fromSystem',
          //   list: 'fromSystemList'
          // },
          { label: '关键字', type: 'input', field: 'name' }
        ]
      },
      tableList: [{}],
      tableFieldList: [
        { label: '名称', param: 'flowName', type: 'slot' },
        { label: '启用状态', param: 'enableName' },
        { label: '发布状态', param: 'statusName' },
        // { label: '接入系统', param: 'fromSystem', type: 'dict' },
        { label: '描述', param: 'description' }
      ],
      // 表格label和value对应表
      labelObj: {
        enableList: [{ value: 1, label: '启用' }, { value: 0, label: '禁用' }],
        statusList: [{ value: 0, label: '下线' }, { value: 1, label: '发布' }, { value: 2, label: '未发布' }],
        fromSystemList: [{ value: 1, label: '是' }]
      }
    }
  },
  created() {
    this.getListData()
  },

  methods: {
    /** 查询角色列表 */
    getListData() {
      //   this.loading = true
      flowPageList(this.queryParams).then(res => {
        this.loading = false
        this.tableList = res.list
        this.total = res.total
        if (this.tableList) {
          this.tableList.forEach(item => {
            item.enableName = this.labelObj.enableList.find(o => o.value === item.enable)?.label
            item.statusName = this.labelObj.statusList.find(o => o.value === item.status)?.label
          })
        }
      })
    },
    /** 搜索按钮操作 */
    handleQuery(value) {
      this.queryParams.current = 1
      this.queryParams.name = value.name
      this.queryParams.enable = value.enable
      this.queryParams.status = value.status
      this.getListData()
    },
    /** 重置按钮*/
    resetQuery() {
      this.queryParams = {
        name: undefined,
        enable: undefined,
        status: undefined,
        size: 10,
        current: 1
      }
      this.getListData()
    },
    /*   多选框选中事件 */
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      //  暂时没有行内删除
      // const id = row.id || this.ids
      const id = this.ids.join(',')
      this.$modal
        .confirm('是否确认删除所选数据项？')
        .then(function() {
          return deleteFlow(id)
        })
        .then(() => {
          this.$modal.msgSuccess('删除成功')
          this.getListData()
        })
        .catch((error) => {
          console.log(error)
        })
    },
    /** 导入按钮操作 */
    async handleImport() {
      await this.setImportParams()
      this.$refs.import_dia.upload.open = true
    },
    async setImportParams() {
      this.$refs.import_dia.upload.headers = undefined
      this.$refs.import_dia.upload.params = 'files'
      this.$refs.import_dia.upload.accept = '.process'
      this.$refs.import_dia.upload.limit = undefined
      this.$refs.import_dia.upload.url = window.baseUrl.ticket + '/flow/import'
      this.$refs.import_dia.fileList = []
    },
    /* 导出操作 */
    handleExport() {
      download(window.baseUrl.ticket + '/flow/export', { 'ids': this.ids, 'formFlag': true }, `${new Date().getTime()}.zip`, '1')
    },
    /** 新建 */
    handleAdd() {
      // sessionStorage.removeItem('processInfo')
      this.$router.push({ path: window.prefix + '/bpmn/processDesign/addProcess' })
    },
    /** 预览 */
    handleView(row) {
      // sessionStorage.setItem('processInfo', JSON.stringify(row))
      this.$router.push({ path: window.prefix + '/bpmn/processDesign/addProcess', query: { id: row.id, type: 'view' }})
    },
    /** 复制 */
    handleCopy(row) {
      // sessionStorage.setItem('processInfo', JSON.stringify(row))
      this.$router.push({ path: window.prefix + '/bpmn/processDesign/addProcess', query: { id: row.id, type: 'copy' }})
    },
    /** 编辑 */
    handleupdate(row) {
      // sessionStorage.setItem('processInfo', JSON.stringify(row))
      this.$router.push({ path: window.prefix + '/bpmn/processDesign/addProcess', query: { id: row.id, type: 'update' }})
    },
    /** 启用 */
    handleEnable(row) {
      flowEnable(row.id).then(res => {
        this.$modal.msgSuccess('已启用')
        this.getListData()
      })
    },
    /** 禁用 */
    handleDisenable(row) {
      flowDisable(row.id).then(res => {
        this.$modal.msgSuccess('已禁用')
        this.getListData()
      })
    },
    /** 下线 */
    handleOffline(row) {
      flowOffline(row.id).then(res => {
        this.$modal.msgSuccess('已下线')
        this.getListData()
      })
    },
    /** 发布 */
    handlePublish(row) {
      flowPublish(row.id).then(res => {
        this.$modal.msgSuccess('已发布')
        this.getListData()
      })
    },
    // 复选框是否禁用
    selectFn(row, index) {
      return row.ref == false
    }
  }
}
</script>
<style lang="scss" scoped>
::v-deep .el-form-item {
  margin-bottom: 0 !important;
}
::v-deep .el-form-item__label {
  display: none !important;
}
.container {
  padding: 0 20px;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
}

::v-deep .el-table .cell {
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  span{
    display:block;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height:23px;
  }
}

.app-container {
  padding: 0;
}

.iconi {
  width: 16px;
  height: 16px;
  display: inline-block;
  text-align: center;
  border-radius: 50%;
  margin-right: 10px;
  line-height: 16px;
  color: white;

  &::before {
    font-size: 10px;
    font-weight: 600;
  }
}
.targetTable {
  flex: 1;
}
</style>
